/*
 * Created on 14.04.2011
 *
 * @author Benedikt
 *
 * createt first in Project Ch@t
 */

package general;

import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

import window.Fenster;
import window.SplitPanel;
import listener.MyMouseListener;

public class Tree extends JTree {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2537756561559109276L;
	public JTree tree;
	Fenster fenster;
	SplitPanel splitPanel;

	public Tree(Fenster fenster, SplitPanel splitPanel) {
		this.fenster = fenster;
		this.splitPanel = splitPanel;
		MyMouseListener mml = new MyMouseListener(this, fenster, splitPanel);
		Object[] hierarchy = {
				"Allgemein",
				"Freund 1",
				"Freund 2",
				"Freund 3",
				"Freund 4",

				new Object[] { "General", "Freund 5", "Freund 6", "Freund 7" },
				new Object[] { "Studium",
						new Object[] { "Professoren", "Prof 1" }, "Freund 8" }, };
		DefaultMutableTreeNode root = processHierarchy(hierarchy);
		tree = new JTree(root);
		add(new JScrollPane(tree), BorderLayout.CENTER);
		tree.addMouseListener(mml);

	}

	private DefaultMutableTreeNode processHierarchy(Object[] hierarchy) {
		DefaultMutableTreeNode node = new DefaultMutableTreeNode(hierarchy[0]);
		DefaultMutableTreeNode child;
		for (int i = 1; i < hierarchy.length; i++) {
			Object nodeSpecifier = hierarchy[i];
			if (nodeSpecifier instanceof Object[]) // Ie node with children
				child = processHierarchy((Object[]) nodeSpecifier);
			else
				child = new DefaultMutableTreeNode(nodeSpecifier); // Ie Leaf
			node.add(child);
		}
		return (node);
	}
}
